package com.tiger.cloud.service.crm;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tiger.cloud.common.exception.ServiceException;
import com.tiger.cloud.common.mybatis.MybatisTigerDao;
import com.tiger.cloud.common.mybatis.TigerBaseService;
import com.tiger.cloud.common.view.SpringUtils;
import com.tiger.cloud.dao.crm.PeersDao;
import com.tiger.cloud.dao.crm.UserCompanyInfoDao;
import com.tiger.cloud.dao.crm.UserDao;
import com.tiger.cloud.dao.crm.UserInfoDao;
import com.tiger.cloud.dao.share.ShareRecipientLogDao;
import com.tiger.cloud.enums.DeleteEnum;
import com.tiger.cloud.enums.PointCodeEnum;
import com.tiger.cloud.enums.UserStatusEnum;
import com.tiger.cloud.form.CompanyForm;
import com.tiger.cloud.form.UserCompanyInfoForm;
import com.tiger.cloud.model.crm.User;
import com.tiger.cloud.model.crm.UserCompanyInfo;
import com.tiger.cloud.model.crm.UserInfo;
import com.tiger.cloud.model.share.PointCodeMessage;
import com.tiger.cloud.model.share.ShareRecipientLog;
import com.tiger.cloud.service.share.ShareRecipientLogService;
import com.tiger.cloud.util.ErrorCode;

/**
 * 用户企业信息
 */
@Service("userCompanyInfoService")
public class UserCompanyInfoService extends TigerBaseService<UserCompanyInfo> implements IUserCompanyInfoService{
    
    @Autowired
	private UserCompanyInfoDao userCompanyInfoDao;
    @Autowired
	private UserInfoDao userInfoDao;
    @Autowired
   	private UserDao userDao;
    @Autowired
    private IUserInfoService userInfoService;

	@Autowired
	private PeersDao peersDao;

	@Autowired
	private ShareRecipientLogDao shareRecipientLogDao;

	@Autowired
	private ShareRecipientLogService shareRecipientLogService;

	@Override
	public MybatisTigerDao<UserCompanyInfo> getEntityDao() {
		return userCompanyInfoDao;
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public UserCompanyInfo register(CompanyForm companyForm) {
		User findUserObj =userDao.get(companyForm.getUserId());
		if(findUserObj==null){
			throw new ServiceException("找不到该用户");
		}
		//findUserObj.setStatus(UserStatusEnum.AUDIT);
		findUserObj.setStatus(UserStatusEnum.NOMAIL);
		findUserObj.setUserName(companyForm.getUsername());
		findUserObj.setHeadImage(companyForm.getHeadImage());
		userDao.update(findUserObj);
		
		UserInfo findUserInfo=new UserInfo();
		findUserInfo.setUserId(companyForm.getUserId());
		findUserInfo.setIsDelete(DeleteEnum.NOMAIL);
//		findUserInfo.setLoginId(findUserObj.getLoginId());
		//用户是否通过分享注册
		ShareRecipientLog shareRecipientLog = new ShareRecipientLog();
		shareRecipientLog.setRecipientMobile(findUserObj.getLoginId());
		List<ShareRecipientLog> shareList = shareRecipientLogDao.findByEntity(shareRecipientLog);

		if(shareList != null && !shareList.isEmpty()){
			shareRecipientLog = shareList.get(0);
			if(companyForm.getUserId() == null){
				shareRecipientLog.setRecipientId(companyForm.getUserId());
			}
			if(shareRecipientLogDao.update(shareRecipientLog)>0){
				boolean flat = shareRecipientLogService.updateSharePoint(findUserObj.getId(),shareRecipientLog.getSharerId(),shareRecipientLog.getSharerInvCode());
				if(!flat){
					throw new ServiceException("用户通过分享注册增加积分失败!");
				}
			}
		}
		UserInfo sUserInfo=userInfoService.findOne(findUserInfo);
		if(sUserInfo!=null){//修改
			sUserInfo.setMobile(findUserObj.getLoginId());
			sUserInfo.setLoginId(findUserObj.getLoginId());
			sUserInfo.setBusinessCard(companyForm.getBusinessCard());
			sUserInfo.setUserName(companyForm.getUsername());
			sUserInfo.setHeadImage(companyForm.getHeadImage());
			if(userInfoDao.update(sUserInfo)>0){
				UserCompanyInfo findUserCompanyInfo=new UserCompanyInfo();
				findUserCompanyInfo.setUserId(sUserInfo.getUserId());
				findUserCompanyInfo.setUserInfoId(sUserInfo.getId());
				UserCompanyInfo userCompanyInfo=findOne(findUserCompanyInfo);
				if(userCompanyInfo!=null){
					userCompanyInfo.setCompanyName(companyForm.getCompanyName());
					userCompanyInfo.setDistrictId(companyForm.getDistrictId());
					userCompanyInfo.setProvinceId(companyForm.getProvinceId());
					userCompanyInfo.setCityId(companyForm.getCityId());
					userCompanyInfo.setProvinceName(companyForm.getProvinceName());
					userCompanyInfo.setDistrictName(companyForm.getDistrictName());
					userCompanyInfo.setCityName(companyForm.getCityName());
					if(userCompanyInfoDao.update(userCompanyInfo)>0){
						return userCompanyInfo;
					}
				}else{
					throw new ServiceException(ErrorCode.NOT_FIND_USER_COMPANY);
				}
			}
		}else{//添加
			UserInfo userInfo=new UserInfo();
			userInfo.setBusinessCard(companyForm.getBusinessCard());
			userInfo.setUserId(companyForm.getUserId());
			userInfo.setUserName(companyForm.getUsername());
			userInfo.setLoginId(findUserObj.getLoginId());
			userInfo.setMobile(findUserObj.getLoginId());
			userInfo.setHeadImage(companyForm.getHeadImage());
			userInfo.setIsDelete(DeleteEnum.NOMAIL);
			if(userInfoDao.insert(userInfo)>0){
				UserCompanyInfo userCompanyInfo=new  UserCompanyInfo();
				userCompanyInfo.setCompanyNo(1);
				userCompanyInfo.setUserId(companyForm.getUserId());
				userCompanyInfo.setUserInfoId(userInfo.getId());
				userCompanyInfo.setCompanyName(companyForm.getCompanyName());
				userCompanyInfo.setDistrictId(companyForm.getDistrictId());
				userCompanyInfo.setProvinceId(companyForm.getProvinceId());
				userCompanyInfo.setCityId(companyForm.getCityId());
				userCompanyInfo.setProvinceName(companyForm.getProvinceName());
				userCompanyInfo.setDistrictName(companyForm.getDistrictName());
				userCompanyInfo.setCityName(companyForm.getCityName());
				if(userCompanyInfoDao.insert(userCompanyInfo)>0){
					return userCompanyInfo;
				}
			}
		}
		throw new ServiceException(ErrorCode.INSERT_DATA_ERROR);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean insertUserCompany(UserCompanyInfo userCompanyInfo) {
		if(userCompanyInfoDao.insert(userCompanyInfo)>0){
			if(SpringUtils.getBean(IUserConsumeService.class).insertRecord(PointCodeEnum.ADD_MUTI_USER_COMPANY.getCode(), userCompanyInfo.getUserId(), 
					PointCodeMessage.getMsg(PointCodeMessage.ADD_MUTI_USER_COMPANY,CommonPointRule.getPoint(PointCodeEnum.ADD_MUTI_USER_COMPANY.getCode()))) !=null){
				return true;
			}
		}
		throw new IllegalArgumentException("用户添加公司异常!");
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean updateCompanyInfo(UserCompanyInfo userCompanyInfo) {
		if(userCompanyInfoDao.update(userCompanyInfo)>0){
			peersDao.updateLastUpdateTime(userCompanyInfo.getUserId());
			return true;
		}
		throw new IllegalArgumentException("修改公司异常!");
	}

	@Override
	public List<UserCompanyInfo> filter(Map<String, Object> paramMap) {
		return userCompanyInfoDao.filter(paramMap);
	}

	@Override
	public List<UserCompanyInfoForm> findFlingEnterprise(Map<String, Object> params) {
		return userCompanyInfoDao.findFlingEnterprise(params);
	}

	@Override
	public List<UserCompanyInfoForm> selectInfo(String userId) {
		return userCompanyInfoDao.selectInfo(userId);
	}

	@Override
	public List<UserCompanyInfoForm> byPersonalOrEnterprise(Map<String, Object> paramMap) {
		return userCompanyInfoDao.byPersonalOrEnterprise(paramMap);
	}

}